package EA;
import EA.testproblems.*; 

public class BitNumericalFramework extends NumericalFramework
{
  /** Array containing number of bits per variable. Each entry should not
      exceed 30 because of possible problems with overflow in integers. 
  */
  public int[] bits_per_variable;
  /** Array containing the maximal value for each of the bitsets. I.e. (2**bits_per_variable[i])-1
   */
  public long[] maxvalue_per_variable;

  /** Total number of bits in all variables. */
  public int totalbits;


  /** Helper variable for generating random crossoverpoints in n-point crossover.*/
  public boolean[] crossoverpoints;

  /** Create a new BitNumericalFramework. This used when the framework is 
      just used to access implementation dependent variables.
  */
  public BitNumericalFramework()
    {
      super();
    }

  /** Create a new BitNumericalFramework. */
  public BitNumericalFramework(Fitness ffunction)
    {
      super(ffunction);
    }

  public BitNumericalFramework(int thedimensions, Fitness ffunction)
    {
      super(thedimensions, ffunction);
    }
  
  /** Create a bitnumerical framework. This sets the bits_per_variable to
      20 for each variable.
  */
  public BitNumericalFramework(int thedimensions, Interval[] theintervals, Fitness ffunction)
    {
      super(thedimensions, theintervals, ffunction);
      bits_per_variable = new int[thedimensions];
      maxvalue_per_variable = new long[thedimensions];
      totalbits = 0;
      for (int i = 0;i<thedimensions;i++) {
	bits_per_variable[i] = 20;
	maxvalue_per_variable[i] = (1<<bits_per_variable[i])-1;
	totalbits = totalbits + bits_per_variable[i];
      }
      
      crossoverpoints = new boolean[totalbits];
    }
  
  public BitNumericalFramework(int thedimensions, Interval[] theintervals, int[] bpv, Fitness ffunction)
    {
      super(thedimensions, theintervals, ffunction);
      bits_per_variable = bpv;
      for (int i = 0;i<thedimensions;i++) {
	maxvalue_per_variable[i] = (1<<bits_per_variable[i])-1;
	totalbits = totalbits + bits_per_variable[i];
      }

      crossoverpoints = new boolean[totalbits];
    }

  /** Create a bitnumerical framework. This sets the bits_per_variable to
      bpv for each variable.
  */
  public BitNumericalFramework(int thedimensions, Interval[] theintervals, int bpv, Fitness ffunction)
    {
      super(thedimensions, theintervals, ffunction);
      bits_per_variable = new int[thedimensions];
      maxvalue_per_variable = new long[thedimensions];
      totalbits = 0;
      for (int i = 0;i<thedimensions;i++) {
	bits_per_variable[i] = bpv;
	maxvalue_per_variable[i] = (1<<bits_per_variable[i])-1;
	totalbits = totalbits + bits_per_variable[i];
      }

      crossoverpoints = new boolean[totalbits];
    }

  /** Create a bitnumerical framework. This sets the bits_per_variable to
      bpv for each variable.
  */
  public BitNumericalFramework(NumericalProblem numproblem, int bpv)
    {
      super(numproblem);
      int thedimensions = numproblem.dimensions;

      bits_per_variable = new int[thedimensions];
      maxvalue_per_variable = new long[thedimensions];
      totalbits = 0;
      for (int i = 0;i<thedimensions;i++) {
	bits_per_variable[i] = bpv;
	maxvalue_per_variable[i] = (1<<bits_per_variable[i])-1;
	totalbits = totalbits + bits_per_variable[i];
      }

      crossoverpoints = new boolean[totalbits];
    }

  /** Create a new BitNumericalFramework.
  */
  public BitNumericalFramework(NumericalProblem numproblem)
    {
      super(numproblem);
      bits_per_variable = new int[dimensions];
      maxvalue_per_variable = new long[dimensions];
      totalbits = 0;
      for (int i = 0;i<dimensions;i++) {
	bits_per_variable[i] = 20;
	maxvalue_per_variable[i] = (1<<bits_per_variable[i])-1;
	totalbits = totalbits + bits_per_variable[i];
      }
      
      crossoverpoints = new boolean[totalbits];
    }


  /** Set the number of bits per variable. 
      @param bpv Bits per variable. Must be less than 31.
  */
  public void setBitsPerVariable(int bpv)
    {
      bits_per_variable = new int[dimensions];
      maxvalue_per_variable = new long[dimensions];
      totalbits = 0;
      for (int i = 0;i<dimensions;i++) {
	bits_per_variable[i] = bpv;
	maxvalue_per_variable[i] = (1<<bits_per_variable[i])-1;
	totalbits = totalbits + bits_per_variable[i];
      }
    }
}


